using System;
using Cysharp.Threading.Tasks;
using Entity.Player;
using MoonFramework.FSM;
using UnityEngine;

namespace Entity.Monster.State
{
    public class MonsterRunState : RunState, IMonster
    {
        public Monster Monster => _actor as Monster;
        protected override async UniTaskVoid Run()
        {
            while (_isRun)
            {
                await UniTask.Yield();
                try
                {
                    if (Vector3.Distance(_actor.viewController.transform.position, PlayerController.Instance.GetPos()) < Monster.MonsterConfig.Radius + Monster.MonsterConfig.AttackRange)
                    {
                        Monster.fsmMachine.ChangeState(nameof(MonsterAttackState)); //可以攻击
                    }
                    else
                    {
                        Monster.MonsterObject.NavMeshAgent.SetDestination(PlayerController.Instance.GetPos());
                        //检测AI的归属地图块
                        ((BoarViewController)Monster.viewController).CheckAndTransferMapBlock();
                    }
                }
                catch
                {}
            }
        }
    }
}